﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using log4net;
using System.IO;
using System.Collections;

namespace CompanyManagement.Controllers
{
    public class AttachmentController : Controller
    {
        //
        // GET: /Attachment/
        AttachmentDLL dllAttachment = new AttachmentDLL();
        TaskDLL dllTask = new TaskDLL();
        private static readonly ILog Log = LogManager.GetLogger(typeof(AttachmentController));

        #region Page Load

        public ActionResult Index(int TaskID)
        {
            TaskMetaData taskmetadta = new TaskMetaData();
            try
            {
                taskmetadta = dllTask.GetTaskByID(TaskID);
                Utility.setCookiValue();
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(taskmetadta);
        }

        public ActionResult loadAttachments(int TaskID)
        {
            List<AttachmentMetaData> attachmentlist = new List<AttachmentMetaData>();
            try
            {
                attachmentlist = dllAttachment.GetAttachmentListByTaskID(TaskID);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return PartialView(attachmentlist);
        }


        #endregion

        #region  Delete

        public ActionResult Delete(int id)
        {
            AttachmentMetaData attachment = new AttachmentMetaData();
            try
            {
                attachment = dllAttachment.GetAttachmentDetailID(id);
                string filepath = Server.MapPath(attachment.Path);

                if (System.IO.File.Exists(filepath))
                {
                    System.IO.File.Delete(filepath);
                }

                dllAttachment.DeleteAttachment(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return RedirectToAction("Index", new { TaskID = attachment.TaskID });
        }

        #endregion

        #region Upload Attachment

        [HttpPost]
        public string Upload(AttachmentMetaData astatch)
        {
            astatch.TaskID = astatch.TaskID;
            string filename = astatch.Path;
            astatch.AttachmentName = astatch.Path.Replace(Path.GetExtension(filename), "");
            astatch.Path = "/Document/" + filename;
            astatch.Detail = astatch.Detail;
            astatch.UploadedBy = Utility.getUserID();
            dllAttachment.AddAttachment(astatch);
            return "Success";
        }

        #endregion

        #region Upload and Download File

        public FileResult Download(int id)
        {
            byte[] fileBytes = null;
            string fileName = string.Empty;
            try
            {
                AttachmentMetaData attachment = new AttachmentMetaData();
                attachment = dllAttachment.GetAttachmentDetailID(id);
                fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~" + attachment.Path));
                fileName = attachment.Path.Replace("/Document/", "");
            }
            catch (Exception ex)
            {

            }
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

        }

        [HttpPost]
        public void UploadAttachment()
        {
            try
            {
                var file = Request.Files;
                foreach (string upload in Request.Files)
                {
                    if (Request.Files[upload].ContentLength == 0) continue;
                    string pathToSave = Server.MapPath("~/Document");
                    if (!Directory.Exists(pathToSave))
                    {
                        Directory.CreateDirectory(pathToSave);
                    }
                    string filename = Path.GetFileName(Request.Files[upload].FileName);
                    Request.Files[upload].SaveAs(Path.Combine(pathToSave, filename));
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
        }

        #endregion


    }
}
